package com.xiaoran.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.xiaoran.springcloud.entitis.CommonResult;
import com.xiaoran.springcloud.entitis.Payment;
import com.xiaoran.springcloud.myhandler.CustomerBlockHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RatelimiteController {

    @GetMapping("/byresource")
    @SentinelResource(value = "byresource",blockHandler = "handleException")
    public CommonResult byResource(){
        return new CommonResult(200,"按资源名称限流测试ok"
                ,new Payment(2020,"serial2001"));
    }


    public CommonResult handleException(BlockException exception){
        return new CommonResult(444,exception.getClass().getCanonicalName()+"\t 服务不可用");
    }




    @GetMapping("/rateLimtite/byurl")
    @SentinelResource(value = "byurl")
    public CommonResult test1(){
        return new CommonResult(200,"按 URL 限流测试ok",new Payment(2020,"serial2001"));
    }




    @GetMapping("/rateLimtite/customerBlockHandler")
    @SentinelResource(value = "customerBlockHandler",
            blockHandlerClass = CustomerBlockHandler.class,
            blockHandler = "handlerException1")
    public CommonResult test2(){
        return new CommonResult(200,"按 CustomerBlockHandler 限流测试ok",new Payment(2020,"serial2004"));
    }
}
